//
//  JNTHomeViewController.m
//  OA
//
//  Created by 王文晓 on 16/8/4.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "JNTHomeViewController.h"
#import "SDCycleScrollView.h"
#import "BaseCollectionViewCell.h"
#import "PersonalNeedCollectionViewCell.h"
#import "ApplyViewController.h"

@interface JNTHomeViewController ()<SDCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView; //滚动视图
@property (nonatomic, strong) UICollectionView *collectionView;   //分区一布局
@property (nonatomic, strong) NSMutableArray *imageArray;        //存储图片
@property (nonatomic, strong) NSMutableArray *titleArray;        //存储标题



@end

@implementation JNTHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"通号移动云平台";
    [self setUpNavgationBar];
   [self.view addSubview:self.cycleScrollView];
    
    [self.view addSubview:self.collectionView];
    
    
    
}






#pragma Mark - -------UICollectionView的代理方法------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return  self.imageArray.count;
   
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PersonalNeedCollectionViewCell *CELL = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
    
    CELL.cellImageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]];
    CELL.cellLabel.text = [self.titleArray objectAtIndex:indexPath.row];
    CELL.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    return CELL;
    
}

//注册头分区
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            
            [headerView addSubview:self.cycleScrollView];
        }
        
        return headerView;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
        return nil;
    }
    return nil;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
   
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        
        return UIEdgeInsetsMake(0, 0, 1, 1);
   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
         return CGSizeMake((kScreenWidth-2)/4, kScreenWidth/4);
    }
    else {
        
        return CGSizeMake(0, 0);
    }
}

//头分区的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    
        return CGSizeMake(kScreenWidth, 210);
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        ApplyViewController *applyVC = [[ApplyViewController alloc] init];
        [self.navigationController pushViewController:applyVC animated:NO];
    }
    ZTLog(@"%ld",indexPath.row);
}

#pragma mark - SDCycleScrollView

- (SDCycleScrollView *)cycleScrollView{
    NSMutableArray *myArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg"], nil];
       if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:kRect(0, 0, kScreenWidth,210) imageNamesGroup:myArray];
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollView.autoScroll = YES;
           _cycleScrollView.delegate = self;
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    }
    return _cycleScrollView;
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    ZTLog(@"%ld",index);
    
    
}










#pragma mark - 设置导航条
- (void)setUpNavgationBar
{
    self.navigationController.navigationBar.translucent = NO;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"上栏"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"通号移动云平台";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (NSMutableArray *)imageArray {
    
    if (!_imageArray) {
        
       _imageArray = [NSMutableArray arrayWithObjects:@"0.jpg",@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg", nil];
    }
    return _imageArray;
}


- (NSMutableArray *)titleArray {
    
    if (!_titleArray) {
        
        _titleArray = [NSMutableArray arrayWithObjects:@"申请",@"公文",@"新闻发布",@"签到",@"访客",@"工作任务",@"日程",@"工作汇报",@"停车",@"用户中心",@"公告栏",@"门禁",@"更多", nil];
    }
    return _titleArray;
}

@end
